//页面回显
var curHost = window.location.host;
/*if(curHost.indexOf('www.fsyuncai') !=-1){
    var disShowSetWarehouseAreaArr = ['GJP0000001','D000000033']; //管家婆和无人仓
}else{
    var disShowSetWarehouseAreaArr = ['GJP0000001','UW00000001'];
}

var parentCustomerCode = sessionStorage.getItem("customerCode");
var isShowSetArea = true;
if(parentCustomerCode && disShowSetWarehouseAreaArr.indexOf(parentCustomerCode) != -1){
    $('#setWareHouseArea').hide();
    $('#clickButton').hide();
    isShowSetArea = false;
}*/

var id = sessionStorage.getItem("id")
var customerId = sessionStorage.getItem('customerid')
var t = 't'
var index = 1
var arr = [];
queryDeliveryCompanyList();//获取发货公司

function Jurisdictions() {
    var getOrderSetting = root.dockingplatform.getOrderRelatedById
    if (settingType === "project") {
        getOrderSetting = root.dockingplatform.getSubOrderRelatedById
    }
    $.ajax({
        type: "POST",
        url: getOrderSetting,
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify({
            "id": id
        }),
        //传入组装的参数
        success: function (data) {
            if (data.errorCode == "0") {
                /* console.log(data, 'kkkk')*/
                if (data.data.separateShippingFee == 10) {
                    //单开
                    $("#open").attr("checked", "checked");
                } else {
                    //不单开
                    $("#noOpen").attr("checked", "checked");
                }
                if (data.data.shippingFeeType == 1) {
                    //免运费
                    $("#mian").attr("checked", "checked");
                    hideDiv(1)
                } else if (data.data.shippingFeeType == 2) {
                    //阶梯运费
                    $("#jieti").attr("checked", "checked");
                    hideDiv(2)
                } else if (data.data.shippingFeeType == 3) {
                    //免运费
                    $("#yuncai").attr("checked", "checked");
                    hideDiv(3)
                }
                if (data.data.warehouseMark==1){
                    //平行仓
                    $("#pingxing").attr("checked","checked");
                }else if (data.data.warehouseMark==0){
                    //指定仓
                    $("#zhiding").attr("checked","checked");
                    $('.deliveryCompany').show();
                    // 指定仓发货公司数据回显
                    /* data.data.designatedWarehouseCompanyId = 756886*/
                    $('#deliveryCompanyLists').val(data.data.designatedWarehouseCompanyId);
                }
                // 取价规则
                data.data.wayGetPrice?$("#price-rules").val(data.data.wayGetPrice):'';//取价规则
                var list = data.data.ladderFreightList;
                if (!!list && list.length > 0) {
                    $.each(list, function (i, item) {
                        $("#classSet1").append("<tr>\n" +
                            "<td id=" + item.level + ">" + item.level + "</td>\n" +
                            "<td>满<input type=\"text\" id=" + t + index + "  oninput= patternNow(this.value,this.id) data-align='' value=" + item.orderAmount + "  class=\"input-text radius ml-10\" style=\"width: 100px\">元</td>" +
                            "<td><input type=\"text\" id=" + t + t + index + "  oninput= patternNow(this.value,this.id) data-align='' value=" + item.freightAmount + "  class=\"input-text radius mr-10\" style=\"width: 100px\">元</td>" +
                            "<td><a id=" + t + t + t + index + " onclick='removeLine(this.id)'>删除</a></td>" +
                            "</tr>");
                        index++;
                        arr.push(item.orderAmount)
                    })
                }
            }else{
                layer.msg(data.msg)
            }
        }
    })
}

function addLine() {
    if (index == 6) {
        $.Huimodalalert('最多设置5级', 1000);
        return;
    }
    var newIndex = index - 1
    if ($("#t" + newIndex).val() == '' || $("#tt" + newIndex).val() == '') {
        $.Huimodalalert('请完善信息', 1000);
        return;
    }

    $("#classSet1").append("<tr>\n" +
        "<td id=" + index + ">" + index + "</td>\n" +
        "<td>满<input type=\"text\"  id=" + t + index + " oninput= patternNow(this.value,this.id) data-align='' class=\"input-text radius ml-10\" style=\"width: 100px\">元</td>" +
        "<td><input type=\"text\" id=" + t + t + index + " oninput= patternNow(this.value,this.id) data-align=''  class=\"input-text radius mr-10\" style=\"width: 100px\">元</td>" +
        "<td><a id=" + t + t + t + index + " onclick='removeLine(this.id)'>删除</a></td>" +
        "</tr>");

    index++
}

function patternNow(value, id) {
    var reg = /^\d+(\.\d{0,2})?$/;
    if (!reg.exec(value)) {
        $("#" + id).val($("#" + id).attr("data-align"))
    } else {
        $("#" + id).attr("data-align", value);
    }
    if (value == "" || value == null) {
        $("#" + id).val("");
        $("#" + id).attr("data-align", value);
    }
}

function hideDiv(value) {
    if (value == 1) {
        $("#table").hide();
        $("#clickButton").hide();
    } else if (value == 2) {
        $("#table").show();
        $("#clickButton").show();
    } else if (value == 3) {
        $("#table").hide();
        $("#clickButton").hide();
    }
}

function removeLine(idx) {
    idx = idx.substring(3)

    $("#" + idx).parent().remove();
    var num = 1;
    $("#classSet1 td[id]").each(function () {
        $("#" + $(this).html()).attr("id", num);
        $("#t" + $(this).html()).attr("id", "t" + num);
        $("#tt" + $(this).html()).attr("id", "tt" + num);
        $("#ttt" + $(this).html()).attr("id", "ttt" + num);
        num++
    })
    $("#classSet1 td[id]").each(function () {
        $("#" + $(this).attr("id")).html($(this).attr("id"));
    })
    index--;
}
// 其他项目的订单设置
function saveData() {
    var noSend = 0
    var freightList = [];
    var numList = [];
    if ($("input[name=yun]:checked").val() == 2) {
        $("#classSet1 td[id]").each(function () {
            $("#" + $(this).attr("id")).html($(this).attr("id"));
            if ($("#t" + $(this).attr("id")).val() == "" ||
                $("#tt" + $(this).attr("id")).val() == ""
            ) {
                noSend = 2;
            }
            var data = {
                "level": $("#" + $(this).attr("id")).html(),
                "orderAmount": $("#t" + $(this).attr("id")).val(),
                "freightAmount": $("#tt" + $(this).attr("id")).val()
            }
            freightList.push(data)
            numList.push($("#t" + $(this).attr("id")).val())
        })
    }
    $.each(numList, function (i, first) {
        $.each(numList, function (s, sed) {
            if (i == s) {
                return true;
            }
            if (first == sed) {
                $.Huimodalalert('存在订单金额相同', 1000);
                noSend = 1;
            }
        })
    })
    if (noSend == 1) {
        return;
    }
    if (noSend == 2) {
        $.Huimodalalert('请填写为空的表格或将这一行删除', 1000);
        return;
    }
    var flag = saveCheck();
    if (!flag) {
        return
    }
    if($("input[name=fahuo]:checked").val() == "" || $("input[name=fahuo]:checked").val() == undefined || $("input[name=fahuo]:checked").val() == null){
        $.Huimodalalert('请选择发货仓设置',1500);
        return;
    }
    // 发货公司参数值designatedWarehouseCompanyId
    // 0 --指定仓  1---平行仓
    var designatedWarehouseCompanyId = null;
    var designatedWarehouseId = null;//云彩仓id
    if($('input[name=fahuo]:checked').val() == 0){
        if(!$('#deliveryCompanyLists option:selected').val()){
            return  layer.msg('请选择发货公司')
        }else{
            designatedWarehouseCompanyId = $('#deliveryCompanyLists option:selected').val();
            designatedWarehouseId = $('#deliveryCompanyLists').find('option:selected').attr('data-attr')
        }
    }else{
        designatedWarehouseCompanyId = null;
        designatedWarehouseId = null;
    }
    var params = {
        id: id,
        separateShippingFee: $("input[name=kai]:checked").val(),
        shippingFeeType: $("input[name=yun]:checked").val(),
        customerId:customerId,//渠道id
        wayGetPrice:$('#price-rules option:selected').val(),//取价规则
        ladderFreightList: freightList,
        warehouseMark:$("input[name=fahuo]:checked").val(),
        designatedWarehouseCompanyId:designatedWarehouseCompanyId,
        designatedWarehouseId:designatedWarehouseId
    }

    $.ajax({
        type: "POST",
        url: root.dockingplatform.orderRelatedSettings,
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(params),
        //传入组装的参数
        success: function (data) {
            if (data.errorCode == "0") {
                $.Huimodalalert('配置已生效', 1000);
                setTimeout(function () {
                    var index = parent.location.reload();
                    parent.layer.close(index);
                }, 700);
            } else {
                $.Huimodalalert('配置失败,请联系客服', 1000);
            }
        }
    })
}
// aic项目的订单设置
function saveSubData() {
    var noSend = 0
    var freightList = [];
    var numList = [];
    if ($("input[name=yun]:checked").val() == 2) {
        $("#classSet1 td[id]").each(function () {
            $("#" + $(this).attr("id")).html($(this).attr("id"));
            if ($("#t" + $(this).attr("id")).val() == "" ||
                $("#tt" + $(this).attr("id")).val() == ""
            ) {
                noSend = 2;
            }
            var data = {
                "level": $("#" + $(this).attr("id")).html(),
                "orderAmount": $("#t" + $(this).attr("id")).val(),
                "freightAmount": $("#tt" + $(this).attr("id")).val()
            }
            freightList.push(data)
            numList.push($("#t" + $(this).attr("id")).val())
        })
    }
    $.each(numList, function (i, first) {
        $.each(numList, function (s, sed) {
            if (i == s) {
                return true;
            }
            if (first == sed) {
                $.Huimodalalert('存在订单金额相同', 1000);
                noSend = 1;
            }
        })
    })
    if (noSend == 1) {
        return;
    }
    if (noSend == 2) {
        $.Huimodalalert('请填写为空的表格或将这一行删除', 1000);
        return;
    }
    var flag = saveCheck();
    if (!flag) {
        return
    }
  /*  if(isShowSetArea && ($("#getArea").find("option:selected").val() ==="0" || $("#getArea").find("option:selected").val() ===0)){
        $.Huimodalalert('请选择发货仓区域',1500);
        return;
    }*/
    if(($("#getArea").find("option:selected").val() ==="0" || $("#getArea").find("option:selected").val() ===0)){
        $.Huimodalalert('请选择发货仓区域',1500);
        return;
    }

    // 发货公司参数值designatedWarehouseCompanyId
    // 0 --指定仓  1---平行仓
    var designatedWarehouseCompanyId = null;
    var designatedWarehouseId = null;//云彩仓id
    if($('input[name=fahuo]:checked').val() == 0){
        if(!$('#deliveryCompanyLists option:selected').val()){
            return  layer.msg('请选择发货公司',{offset:'t'})
        }else{
            designatedWarehouseCompanyId = $('#deliveryCompanyLists option:selected').val();
            designatedWarehouseId = $('#deliveryCompanyLists').find('option:selected').attr('data-attr')
        }
    }else{
        designatedWarehouseCompanyId = null;
        designatedWarehouseId = null;
    }
    var parame = {
        id: id,
        separateShippingFee: $("input[name=kai]:checked").val(),
        shippingFeeType: $("input[name=yun]:checked").val(),
        ladderFreightList: freightList,
        warehouseMark:$("input[name=fahuo]:checked").val(),
        designatedWarehouseCompanyId:designatedWarehouseCompanyId,
        designatedWarehouseId:designatedWarehouseId
    };

  /*  if(isShowSetArea){
        parame.priceAreaId = $("#getArea").find("option:selected").val();
        parame.priceAreaName = $("#getArea").find("option:selected").html();
    }*/
/*    parame.priceAreaId = $("#getArea").find("option:selected").val();
    parame.priceAreaName = $("#getArea").find("option:selected").html();*/
    $.ajax({
        type: "POST",
        url: root.dockingplatform.subOrderRelatedSettings,
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(parame),
        //传入组装的参数
        success: function (data) {
            if (data.errorCode == "0") {
                $.Huimodalalert('配置已生效', 1000);
                setTimeout(function () {
                    var index = parent.location.reload();
                    parent.layer.close(index);
                }, 700);
            } else {
                $.Huimodalalert('配置失败,请联系客服', 1000);
            }
        }
    })
}

// 保存校验规则必填项
// customerCode == GJP0000001 是管家婆
function saveCheck() {
    var freightTypeValue = $("input[name = kai]:checked").val();//运费类型
    var freightSetValue = $("input[name = yun]:checked").val();//运费设置
    var priceRulesValue = $("#price-rules").find("option:selected").val();// 取价规则
    var customerCode = sessionStorage.getItem('customerCode');
    if (!freightTypeValue) {
        layer.msg('请选择运费类型', {offset: "t"});
        return false;
    } else if (!freightSetValue) {
        layer.msg('请选择运费设置', {offset: "t"});
        return false;
    }  else if (!priceRulesValue && customerCode!='GJP0000001' ) {
        layer.msg('请选择取价规则', {offset: "t"});
        return false;
    }  else {
        return true
    }
}

// 监听radio事件
$('input[type=radio][name=fahuo]').change(function(){
    // 1 平行仓   0 指定仓
    if($(this).val() == 0){
        $('.deliveryCompany').show();
    }else{
        $('.deliveryCompany').hide();
    }
});

//获取发货公司
function queryDeliveryCompanyList(){
    var successData = function(data){
        if(data.errorCode == 0){
            var result = data.data;
            if(result && result.length >0){
                var channelItem =  '';
                result.forEach(function(item,index){
                    channelItem +="<option value='"+item.companyId+"'  data-attr='"+item.warehouseId+"'>"+item.companyName+"</option>"
                });
                var str = '<option value="" selected>请选择发货公司</option>'
                $('#deliveryCompanyLists').html(str+channelItem)
            }else{
                var empty = '<option value="" selected>请选择发货公司</option><option value="">暂无数据</option>';
                $('#deliveryCompanyLists').html(empty)
            }
            Jurisdictions()
        }else{
            layer.msg(data.msg)
        }
    }
        return web.doAjax(root.dockingplatform.queryDeliveryCompanyList, 'post', {}, successData, 1);
}


